@echo off
title Simple Mod Manager
setlocal enabledelayedexpansion

REM Step 1: Check if the current folder is named b1
for %%I in ("%cd%") do (
    if /I "%%~nI" neq "b1" (
        echo Please place this script in the "b1" folder of the game path to start.
        pause
        exit /b
    )
)

:main
cls
REM Main Menu: Choose an option
echo.
echo Please select an operation:
echo 1. Manage Lua Mod
echo 2. Manage Pak Mod
echo 3. Exit Program
set /p choice_main="Enter your choice (1-3): "
if "%choice_main%"=="1" (
    goto manage_lua_mod
) else if "%choice_main%"=="2" (
    goto manage_pak_mod
) else if "%choice_main%"=="3" (
    echo Exiting program...
    pause
    exit /b
) else (
    echo Invalid input, please try again.
    goto main
)

:manage_lua_mod
cls
echo Detecting installed Lua Mods...
echo Disabled Lua Mods are removed from the enabled.txt file to disable them.
set "mod_dir=.\Binaries\Win64\ue4ss\Mods"
setlocal enabledelayedexpansion
set count=0

REM Exclude folders that should not be checked
for /d %%I in ("%mod_dir%\*") do (
    set "folder=%%~nxI"
    if /I "!folder!" neq "ActorDumperMod" if /I "!folder!" neq "AllowModsMod" if /I "!folder!" neq "BPML_GenericFunctions" if /I "!folder!" neq "BPModLoaderMod" if /I "!folder!" neq "CheatManagerEnablerMod" if /I "!folder!" neq "ConsoleCommandsMod" if /I "!folder!" neq "ConsoleEnablerMod" if /I "!folder!" neq "jsbLuaProfilerMod" if /I "!folder!" neq "Keybinds" if /I "!folder!" neq "KismetDebuggerMod" if /I "!folder!" neq "LineTraceMod" if /I "!folder!" neq "shared" if /I "!folder!" neq "SplitScreenMod" (
        set /a count+=1
        set "enabled=!mod_dir!\!folder!\enabled.txt"
        set "main_lua=!mod_dir!\!folder!\Scripts\main.lua"
        
        if exist "!enabled!" (
            if exist "!main_lua!" (
                echo !count!. [Lua MOD] Active: !folder!
            ) else (
                echo !count!. [Lua MOD] Error: !folder!
            )
        ) else (
            if exist "!main_lua!" (
                echo !count!. [Lua MOD] Inactive: !folder!
            ) else (
                echo !count!. [Lua MOD] Error: !folder!
            )
        )
        set "mod_list[!count!]=!folder!"
    )
)

if !count! equ 0 (
    echo No manageable Lua Mods found.
    pause
    goto main
)

:lua_mod_selection
echo.
set /p lua_choice="Enter the mod number to toggle (enter b to return to the main menu): "

if /I "%lua_choice%"=="b" (
    goto main
)

if not defined mod_list[%lua_choice%] (
    echo Invalid input, please try again.
    goto lua_mod_selection
)

set "selected_mod=!mod_list[%lua_choice%]!"
set "enabled_file=%mod_dir%\!selected_mod!\enabled.txt"

if not exist "%mod_dir%\!selected_mod!\Scripts\main.lua" (
    echo [Error] The mod is corrupted and cannot be toggled.
    pause
    goto lua_mod_selection
)

if exist "%enabled_file%" (
    echo Disabling mod: !selected_mod!
    del /q "%enabled_file%"
) else (
    echo Enabling mod: !selected_mod!
    echo. > "%enabled_file%"
)

goto manage_lua_mod
:manage_pak_mod
cls
echo Detecting installed Pak Mods...
echo Disabled Pak Mods are stored in the b1\PakMod_bak folder.
set "pak_dir=.\Content\Paks\~mods"
set "bak_dir=.\PakMod_bak"
set count=1

REM Create bak directory if it doesn't exist
if not exist "%bak_dir%" mkdir "%bak_dir%"

REM Check files in pak directory
for %%I in ("%pak_dir%\*.pak") do (
    set "file=%%~nxI"
    echo !count!. [Pak MOD] Active: !file!
    set "pak_list[!count!]=!file!"
    set /a count+=1
)

REM Check files in bak directory
for %%I in ("%bak_dir%\*.pak") do (
    set "file=%%~nxI"
    echo !count!. [Pak MOD] Inactive: !file!
    set "pak_list[!count!]=!file!"
    set /a count+=1
)

if %count% equ 1 (
    echo No Pak Mods found.
    pause
    goto main
)

:pak_mod_selection
echo.
set /p pak_choice="Enter the mod number to toggle (enter b to return to the main menu): "

if /I "%pak_choice%"=="b" (
    goto main
)

if not defined pak_list[%pak_choice%] (
    echo Invalid input, please try again.
    goto pak_mod_selection
)

set "selected_pak=!pak_list[%pak_choice%]!"
set "full_path=%pak_dir%\!selected_pak!"
set "bak_path=%bak_dir%\!selected_pak!"

REM Move files between active and inactive directories
if exist "%full_path%" (
    echo Disabling mod: !selected_pak!
    move /y "%full_path%" "%bak_path%"
) else (
    echo Enabling mod: !selected_pak!
    move /y "%bak_path%" "%full_path%"
)

goto manage_pak_mod
